﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.huawei.esdk.tp.professional.DataType;
using System.Web.Services.Protocols;
using System.Collections;
using com.huawei.esdk.tp.professional.Utils;
using com.huawei.esdk.tp.professional.eSDKTPProfessionalSiteMgr;

namespace com.huawei.esdk.tp.professional.local
{
    public partial class SiteServiceEx
    {
        private static string url = com.huawei.esdk.tp.professional.Utils.UtilsSettings.URL + com.huawei.esdk.tp.professional.Utils.UtilsSettings.tpProfessionalSiteMgr;
        private static TPProfessionalSiteMgrService siteMgr = new TPProfessionalSiteMgrService(url);
        EncryptUtiles encryptUtiles = new EncryptUtiles();

        private static SiteServiceEx instance = null;
        protected SiteServiceEx()
        {

        }
        public static SiteServiceEx Instance()
        {
            if (null == instance)
            {
                instance = new SiteServiceEx();
            }
            return instance;
        }
        public TPSDKResponseEx<int?> isConnectAuxSourceEx(string siteUri)
        {
            siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
            TPSDKResponseEx<int?> resp = new TPSDKResponseEx<int?>();
            try
            {
                int result;
                bool resultSpecified;
                int resultCode = siteMgr.isConnectAuxSourceEx(siteUri, out result, out resultSpecified);

                resp.resultCode = resultCode;
                if (0 == resultCode)
                {
                    resp.result = result;
                }
                else
                {
                    resp.result = null;
                }
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }

        }

        public TPSDKResponseEx<SiteDeviceVersionInfoEx> querySiteVersionInfoEx(string siteUri)
        {
            siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
            TPSDKResponseEx<SiteDeviceVersionInfoEx> resp = new TPSDKResponseEx<SiteDeviceVersionInfoEx>();
            SiteDeviceVersionInfoEx siteDeviceVersionInfoEx = new SiteDeviceVersionInfoEx();
            try
            {
                int resultCode = siteMgr.querySiteVersionInfoEx(siteUri, out siteDeviceVersionInfoEx);
             
                resp.resultCode = resultCode;
                resp.result = 0 == resultCode ? siteDeviceVersionInfoEx : null;
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }
        }

        public TPSDKResponseEx<int?> isSendAuxStreamEx(string siteUri)
        {
            siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
            TPSDKResponseEx<int?> resp = new TPSDKResponseEx<int?>();
            try
            {
                int result;
                bool resultSpecified;
                int resultCode = siteMgr.isSendAuxStreamEx(siteUri, out result, out resultSpecified);

                resp.resultCode = resultCode;
                if (0 == resultCode)
                {
                    resp.result = result;
                }
                else
                {
                    resp.result = null;
                }
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }

        }

        public TPSDKResponseEx<int?> isReceiveRemAuxStrmEx(string siteUri)
        {
            siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
            TPSDKResponseEx<int?> resp = new TPSDKResponseEx<int?>();
            try
            {
                int result;
                bool resultSpecified;
                int resultCode = siteMgr.isReceiveRemAuxStrmEx(siteUri, out result, out resultSpecified);

                resp.resultCode = resultCode;
                if (0 == resultCode)
                {
                    resp.result = result;
                }
                else
                {
                    resp.result = null;
                }
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }

        }

        public int setAuxStreamEx(string siteUri, int actionCode)
        {
            try
            {
                siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
                int resultCode = siteMgr.setAuxStreamEx(siteUri, actionCode);
                return resultCode;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }
        }

        public TPSDKResponseEx<Dictionary<int, string>> queryAuxStreamSourcesEx(string siteUri)
        {
            siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
            TPSDKResponseEx<Dictionary<int, string>> resp = new TPSDKResponseEx<Dictionary<int, string>>();
            try
            {
                int resultCode;
                AuxStreamInfoEx[] auxStream;
                resultCode = siteMgr.queryAuxStreamSourcesEx(siteUri, out auxStream);
                if (0 == resultCode)
                {
                    Dictionary<int, string> dictionary = new Dictionary<int, string>();
                    foreach (AuxStreamInfoEx aux in auxStream)
                    {
                        dictionary.Add(aux.id, aux.name);
                    }
                    resp.result = dictionary;
                }
                else 
                {
                    resp.result = null;
                }
                resp.resultCode = resultCode;
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }

        }

        public TPSDKResponseEx<List<VideoSourcesInfoEx>> queryVideoOutSrcStateEx(string siteUri)
        {
            siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
            TPSDKResponseEx<List<VideoSourcesInfoEx>> resp = new TPSDKResponseEx<List<VideoSourcesInfoEx>>();
            try
            {
                int resultCode;
                VideoSourcesInfoEx[] videoSourcesInfoExs;

                resultCode = siteMgr.queryVideoOutSrcStateEx(siteUri, out videoSourcesInfoExs);
                if (0 == resultCode)
                {
                    List<VideoSourcesInfoEx> list = new List<VideoSourcesInfoEx>();
                    foreach (VideoSourcesInfoEx videoSourcesInfoEx in videoSourcesInfoExs)
                    {
                        list.Add(videoSourcesInfoEx);
                    }
                    resp.result = list;
                }
                else
                {
                    resp.result = null;
                }
                resp.resultCode = resultCode;
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }

        }

        public int ctrlCameraEx(string siteUri, CameraControlEx cameraControl)
        {
            try
            {
                siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
                if (null != cameraControl && null != cameraControl.camPos)
                {
                    cameraControl.camPosSpecified = true; 
                }
                int resultCode = siteMgr.ctrlCameraEx(siteUri, cameraControl);
                return resultCode;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }


        }

        public int setMainAuxStreamSourcesEx(string siteUri, List<int?> localMainSrc, int localAuxSrc)
        {
            try
            {
                siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
                int?[] localMainSrcs = null;
                if (null != localMainSrc)
                { 
                  localMainSrcs = localMainSrc.ToArray<int?>();
                }
                int resultCode = siteMgr.setMainAuxStreamSourcesEx(siteUri, localMainSrcs, localAuxSrc);
                return resultCode;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }
        }

        public TPSDKResponseEx<Dictionary<int, string>> queryMainStreamSourcesEx(string siteUri)
        {
            siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
            TPSDKResponseEx<Dictionary<int, string>> resp = new TPSDKResponseEx<Dictionary<int, string>>();
            try
            {
                int resultCode;
                AuxStreamInfoEx[] auxStream;

                resultCode = siteMgr.queryMainStreamSourcesEx(siteUri, out auxStream);
                if (0 == resultCode)
                {
                    Dictionary<int, string> dictionary = new Dictionary<int, string>();
                    foreach (AuxStreamInfoEx aux in auxStream)
                    {
                        dictionary.Add(aux.id, aux.name);
                    }
                    resp.result = dictionary;
                }
                resp.resultCode = resultCode;
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }
        }

        public int setVideoOutSrcEx(string siteUri, int hdOut, int videoSrc)
        {
            try
            {
                siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
                int resultCode;


                resultCode = siteMgr.setVideoOutSrcEx(siteUri, hdOut, videoSrc);
                return resultCode;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }

        }

        // 2015年1月22日 新增接口

        //添加会场信息
        public int addSiteInfoEx(string orgId, TerminalInfoEx siteInfo)
        {
            try
            {
                siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;

                if (siteInfo.regPassword != null && siteInfo.regPassword != "")
                {
                    siteInfo.regPassword = encryptUtiles.EncryptMethod(siteInfo.regPassword);
                    //siteInfo.regPassword = rsa2048.AESEncrypt(siteInfo.regPassword, AuthorizeServiceEx.secretKey, AuthorizeServiceEx.iv);
                }

                int resultCode = siteMgr.addSiteInfoEx(orgId, siteInfo);
                return resultCode;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }
        }

        //删除会场信息
        public int deleteSiteInfoEx(string[] siteUris)
        {
            try
            {
                siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
                int resultCode = siteMgr.deleteSiteInfoEx(siteUris);
                return resultCode;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }
        }

        //编辑会场信息
        public int editSiteInfoEx(TerminalInfoEx siteInfo)
        {
            try
            {
                siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;

                if (siteInfo.regPassword != null && siteInfo.regPassword != "")
                {
                    siteInfo.regPassword = encryptUtiles.EncryptMethod(siteInfo.regPassword);
                    //siteInfo.regPassword = rsa2048.AESEncrypt(siteInfo.regPassword, AuthorizeServiceEx.secretKey, AuthorizeServiceEx.iv);
                }

                int resultCode = siteMgr.editSiteInfoEx(siteInfo);
                return resultCode;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }
        }

        //查询会场信息
        public TPSDKResponseEx<QuerySitesInfoExResponse> querySitesInfoEx(QueryConfigEx queryConfig)
        {
            siteMgr.CookieContainer = AuthorizeServiceEx.cookieJar;
            TPSDKResponseEx<QuerySitesInfoExResponse> resp = new TPSDKResponseEx<QuerySitesInfoExResponse>();
            try
            {
                TerminalInfoEx[] sites;
                PagesInfoEx pageInfo = new PagesInfoEx();

                if (queryConfig.focusItem != null)
                {
                    queryConfig.focusItemSpecified = true;
                }

                int resultCode = siteMgr.querySitesInfoEx(queryConfig, out sites, out pageInfo);

                resp.resultCode = resultCode;
                if (resultCode == 0)
                {
                    QuerySitesInfoExResponse response = new QuerySitesInfoExResponse();

                    response.sites = sites;
                    response.pageInfo = pageInfo;

                    resp.result = response;
                }
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }
        }


    }
}
